package com.iretailer.report.web.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iretailer.report.base.BaseAction;
import com.iretailer.report.dto.PageResult;
import com.iretailer.report.dto.QueryOption;
import com.iretailer.report.dto.RePageWidgets;
import com.iretailer.report.model.PageWidgetCompany;
import com.iretailer.report.model.PageWidgets;
import com.iretailer.report.model.User;
import com.iretailer.report.util.RMap;
import com.iretailer.report.util.SecurityUserUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Auther: xjw
 * @Date: 2019/1/28 09:20
 * @Description:
 */
@RestController
public class PageWidgetController extends BaseAction {

    @RequestMapping("/system/SelectPageWidgetManager.action")
    public Map<String, Object> select(@RequestBody RePageWidgets rePageWidgets) {
        Integer mobile = rePageWidgets.getMobile();
        List<PageWidgets> result = pageWidgetService.list(new QueryWrapper<PageWidgets>().lambda().eq(PageWidgets::getMobile, mobile).orderByDesc(PageWidgets::getRanked));
        return RMap.ok(result);
    }

    @RequestMapping("/system/gridPageWidgetManager.action")
    public Map<String, Object> gridPageWidgetManager(@RequestBody RePageWidgets rePageWidgets, HttpServletRequest request) {
         User user = SecurityUserUtil.currentUser(request);
        QueryOption options = modelMapper.map(rePageWidgets, QueryOption.class);
        PageResult<PageWidgetCompany> find = pageWidgetCompanyService.selectPageByIsModel_Title(rePageWidgets.getMobile(), rePageWidgets.getTitle(), options);
        List<RePageWidgets> list = new ArrayList<>();
        if (find.getList() != null) {
            String companId = user.getCompanyId();
            for (PageWidgetCompany pw : find.getList()) {
                RePageWidgets bap = modelMapper.map(pw, RePageWidgets.class);
                switch (companId) {
                    case "-":
                        bap.setEditable(true);
                        break;
                    default:
                        if (companId.equals(pw.getCompanyId())) {
                            bap.setEditable(true);
                        }
                        break;
                }
                list.add(bap);
            }
        }

        return RMap.ok(new PageResult(find.getTotal(), list));
    }

    @RequestMapping("/system/deletePageWidget.action")
    public Map<String, Object> delete(@RequestBody List<Long> ids) {
        boolean result = pageWidgetService.removeByIds(ids);
        return RMap.resultBoolean(result);
    }

    @RequestMapping("/system/SavePageWidget.action")
    public Map<String, Object> save(@RequestBody RePageWidgets rePageWidgets) {
//        ModelMapper modelMapper = new ModelMapper();
        PageWidgets pageWidgets = modelMapper.map(rePageWidgets, PageWidgets.class);
        boolean result = pageWidgetService.saveOrUpdate(pageWidgets);
        if (result) {
            return RMap.ok();
        } else {
            return RMap.error();
        }
    }


}
